package com.jumi.microservice.stock;

import com.jumi.microservice.constant.GoodsTypeEnum;
import com.jumi.microservice.constant.InventoryType;
import com.jumi.microservice.constant.NumberConstant;
import com.jumi.microservice.domain.GoodsStockDO;
import com.jumi.microservice.domain.GoodsStockLogDO;
import com.jumi.microservice.domain.GoodsWarehouseStockDO;
import com.jumi.microservice.domain.OrderItemDTO;
import com.jumi.microservice.mapper.GoodsStockLogMapper;
import com.jumi.microservice.mapper.GoodsStockMapper;
import com.jumi.microservice.mapper.GoodsWarehouseStockMapper;

import java.util.List;

/**
 * - - - - - - - - - - - - - - -
 * 取消订单操作组件
 * @author liuz
 * @date 2020/9/8 16:06
 * ToDoSomeThing
 * - - - - - - - - - - - - - - -
 */
public class CancelOrderUpdater extends AbstractStockUpdater {

	private OrderItemDTO orderItemDTO;
	
	/**
	 * 构造函数
	 * @param goodsStock 商品库存对象
	 * @param goodsWarehouseStocks 商品分仓库存对象
	 * @param goodsStockLog 库存流水对象
	 * @param goodsStockMapper
	 * @param goodsWarehouseStockMapper
	 * @param goodsStockLogMapper
	 */
	public CancelOrderUpdater(GoodsStockDO goodsStock,
							  GoodsStockLogDO goodsStockLog,
							  GoodsWarehouseStockDO goodsWarehouseStocks,
							  GoodsStockMapper goodsStockMapper,
							  GoodsWarehouseStockMapper goodsWarehouseStockMapper,
							  GoodsStockLogMapper goodsStockLogMapper,
							  OrderItemDTO orderItemDTO) {
		super(goodsStock,goodsWarehouseStocks,goodsStockLog,goodsStockMapper,goodsWarehouseStockMapper,goodsStockLogMapper);
		this.orderItemDTO = orderItemDTO;
	}

	/**
	 * 更新销售库存
     * 未支付:销售库存+ 锁定库存- 已支付:销售库存+ 锁定库存- 已销-
	 */
	@Override
	protected Integer updateSaleStockQuantity() throws Exception {
	    //处理自营的销售库存
	    if(GoodsTypeEnum.GOODS_TYPE_THREE.getCode()!=orderItemDTO.getIsShelf()){
			if(goodsWarehouseStocks!=null) {
				goodsWarehouseStocks.setSaleStockQuantity(goodsWarehouseStocks.getSaleStockQuantity() + orderItemDTO.getPurchaseQuantity());
				return orderItemDTO.getPurchaseQuantity();
			}
        }
	    return null;
	}
	
	/**
	 * 更新锁定库存
     * 未支付:销售库存+ 锁定库存- 已支付:销售库存+ 锁定库存- 已销-
	 */
	@Override
	protected Integer updateLockedStockQuantity() throws Exception {
		goodsStock.setLockedStorage(goodsStock.getLockedStorage()-orderItemDTO.getPurchaseQuantity());
		goodsStockLog.setLockedStorage(goodsStockLog.getLockedStorage()-orderItemDTO.getPurchaseQuantity());
        //处理自营的销售库存
        if(GoodsTypeEnum.GOODS_TYPE_THREE.getCode()!=orderItemDTO.getIsShelf()){
        	//判断不为空
			if(goodsWarehouseStocks!=null) {
				goodsWarehouseStocks.setLockedStockQuantity(goodsWarehouseStocks.getLockedStockQuantity() - orderItemDTO.getPurchaseQuantity());
				return -(orderItemDTO.getPurchaseQuantity());
			}
        }
        return null;
	}
	
	/**
	 * 更新已销售库存
     * 未支付:销售库存+ 锁定库存- 已支付:销售库存+ 锁定库存- 已销-
	 */
	@Override
	protected Integer updateSaledStockQuantity() throws Exception {
	    //已支付的处理已销售
        if(orderItemDTO.getOrderStatus()!=null&&1==orderItemDTO.getOrderStatus()) {
        	//已售-
			goodsStock.setSaledStorage(goodsStock.getSaledStorage() - orderItemDTO.getPurchaseQuantity());
            //处理自自营
            if (GoodsTypeEnum.GOODS_TYPE_THREE.getCode()!=orderItemDTO.getIsShelf()) {
                //已支付 处理已销售 判断查询的是否为空
				if(goodsWarehouseStocks!=null) {
					goodsWarehouseStocks.setSaledStockQuantity(goodsWarehouseStocks.getSaledStockQuantity() - orderItemDTO.getPurchaseQuantity());
					return -(orderItemDTO.getPurchaseQuantity());
				}
            }
        }
        return null;
	}

	/**
     * 更新商品的预售库存
	 * 20201219 只更新订单走销售
     */
    @Override
    protected Integer updatePreStockQuantity() throws Exception {
        //处理预售商品
        if(orderItemDTO.getIsPre()!=null&&  NumberConstant.NUMBER_ONE.intValue()==orderItemDTO.getIsPre()){
            goodsStock.setPreStorage(goodsStock.getPreStorage()+orderItemDTO.getPurchaseQuantity());
			return orderItemDTO.getPurchaseQuantity();
        }
        return null;
    }

    /**
     * 更新商品的总库存
	 * 20201219 只更新订单走实际库存
     */
    @Override
    protected Integer updateSkuStorageQuantity() throws Exception {
    	if(orderItemDTO.getIsPre()!=null&& NumberConstant.NUMBER_ONE.intValue()!=orderItemDTO.getIsPre()) {
			goodsStock.setGoodsSkuStorage(goodsStock.getGoodsSkuStorage() + orderItemDTO.getPurchaseQuantity());
			return orderItemDTO.getPurchaseQuantity();
		}
    	return null;
    }

}
